package zj.com.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import zj.com.adapter.DjcAdapter;
import zj.com.adapter.DshAdapter;
import zj.com.model.DjcModel;
import zj.com.model.Dzg;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.DialogUtil;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/3.
 * 待审核
 */
public class DshListActivity extends  BaseActivity implements View.OnClickListener{


    public TextView title_name;
    public ImageView left_iv;
    public TextView right_tv,cancel_tv,submit_tv,del_tv;
    public ImageView right_iv;
    public ListView listView;
    public List<Dzg> list;
    public DshAdapter adapter;
    public Intent intent;


    public LinearLayout bottom_ll;
    public RelativeLayout error_rl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dsh_list);
        initView();
        initData();
    }

    public void initView(){

        error_rl = (RelativeLayout) findViewById(R.id.error_rl);
        del_tv = (TextView) findViewById(R.id.del_tv);
        del_tv.setOnClickListener(this);
        bottom_ll = (LinearLayout) findViewById(R.id.bottom_ll);
        cancel_tv = (TextView) findViewById(R.id.cancel_tv);
        cancel_tv.setOnClickListener(this);
        bottom_ll = (LinearLayout) findViewById(R.id.bottom_ll);
        submit_tv = (TextView) findViewById(R.id.submit_tv);
        submit_tv.setOnClickListener(this);


        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setVisibility(View.VISIBLE);
        left_iv.setOnClickListener(this);
        title_name = (TextView) findViewById(R.id.title_name);
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setVisibility(View.VISIBLE);
        right_tv.setOnClickListener(this);
        right_tv.setText("编辑");
        right_tv.setVisibility(View.GONE);
        title_name.setText("待审核任务");

        right_iv = (ImageView) findViewById(R.id.right_iv);
        right_iv.setVisibility(View.VISIBLE);
        right_iv.setImageResource(R.drawable.edit_icon);
        right_iv.setOnClickListener(this);
        right_iv.setVisibility(View.GONE);

        listView = (ListView) findViewById(R.id.listView);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                LogUtils.getInstance().error("click");
                        intent = new Intent(DshListActivity.this, DshDetailActivity.class);
                intent.putExtra("detail", list.get(position));
                startActivity(intent);
            }
        });


    }

    public void initData(){

        list = new ArrayList<Dzg>();
        adapter = new DshAdapter(list,this);
        listView.setAdapter(adapter);

    }

    @Override
    protected void onResume() {
        super.onResume();
        obtainData();
    }
   public ProgressDialog dialog;
    public void obtainData(){
        dialog = DialogUtil.createProgressDialog(this,"提示","数据获取中..");
        NetManager.getInstance(this).obtainGetPenddingAuditRepairRecordList(new OnResponseListener<List<Dzg>>() {
            @Override
            public void onComplete(boolean isOK, List<Dzg> result, int errorCode, String errorMsg) {
                dialog.dismiss();
                if(isOK){
                    list = result;
                    if(list != null && list.size() > 0){
                        error_rl.setVisibility(View.GONE);
                    }else{
                        error_rl.setVisibility(View.VISIBLE);

                    }
                    adapter.setDataChange(list);
                }else{
                    error_rl.setVisibility(View.VISIBLE);

                    Toast.makeText(DshListActivity.this,"获取失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                error_rl.setVisibility(View.VISIBLE);
                Toast.makeText(DshListActivity.this,"获取失败",Toast.LENGTH_SHORT).show();
                dialog.dismiss();

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.right_iv:

                break;
            case R.id.cancel_tv:
                bottom_ll.setVisibility(View.GONE);
                right_tv.setVisibility(View.VISIBLE);
                adapter.setIsEdit(false);
                adapter.getSelectMap().clear();
                adapter.notifyDataSetChanged();
                adapter.setSelect(-1);
                break;
            case R.id.submit_tv:
                bottom_ll.setVisibility(View.GONE);
                right_tv.setVisibility(View.VISIBLE);
                adapter.setIsEdit(false);

                break;

            case R.id.right_tv:
                bottom_ll.setVisibility(View.VISIBLE);
                right_tv.setVisibility(View.GONE);
                adapter.setIsEdit(true);

                break;
            case R.id.del_tv:
                bottom_ll.setVisibility(View.GONE);
                right_tv.setVisibility(View.VISIBLE);
                adapter.setIsEdit(false);
                break;

        }

    }
}
